﻿using Coldairarrow.Business.Interface;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.Interface;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Math;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Reflection;
using System.Security.Principal;
using System.Threading.Tasks;
using static AutoMapper.Internal.CollectionMapperExpressionFactory;

namespace Coldairarrow.Business.Base_Manage
{
    /// <summary>
    ///操作OL采购需求业务逻辑
    /// </summary>
    public class OL_ProRequirementsBusiness
    {
        public IDbAccessor Db { get; set; }
        /// <summary>
        /// 操作ol采购需求对象
        /// </summary>
        public OL_ProRequirementsInterfaceBusiness _OL_ProRequirementsInterface { get; set; }

        public OL_ProRequirementsBusiness(IDbAccessor db)
        {
            this.Db = db;
            _OL_ProRequirementsInterface = new OL_ProRequirementsInterfaceBusiness();
        }

        /// <summary>
        /// 更新行
        /// </summary>
        /// <param name="newItems"></param>
        /// <returns></returns>
        public async Task EditRows(ProRequirementsDTO Items)
        {
            if (!Items.Item.IsNullOrEmpty() && Items.Item.Count > 0)
            {
                var first = Items.Item.First();
                if (first == null)
                {

                }
                //获取采购需求
                _OL_ProRequirementsInterface.Filters.Clear();

                foreach (var item1 in Items.Item)
                {
                    _OL_ProRequirementsInterface.Filters.Add(
                    new OL_FilterDTO() { ControlId = "rowid", DataType = 2, SpliceType = 2, FilterType = 2, Value = item1.RowId }//根据rowId查询对应的行更新
                );
                }
                List<OL_ProRequirements> sourceDatas = await _OL_ProRequirementsInterface.QueryAsync<OL_ProRequirements>(_OL_ProRequirementsInterface.queryOption, _OL_ProRequirementsInterface.tableField, _OL_ProRequirementsInterface.SqlDb);
                if (sourceDatas.Count > 0)
                {


                    //将需要更新的字段赋值
                    foreach (var item2 in Items.Item)
                    {
                        var row = sourceDatas.Where(x => x.RowId == item2.RowId);
                        if (!row.IsNullOrEmpty() && row.Count() > 0)
                        {
                            var temp = row.First();

                            if (item2.CheckedResult == "不合格")
                            {
                                temp.CheckedResult = item2.CheckedResult;
                                temp.CheckState = "";
                            }
                            else
                            {
                                temp.CheckedResult = item2.CheckedResult;
                                temp.CheckState = "已完成";
                            }

                        }

                    }

                    //过滤需要更新的字段
                    _OL_ProRequirementsInterface.tableField = _OL_ProRequirementsInterface.tableField.Where(x => x.Value == "CheckState" || x.Value == "RowId" || x.Value == "CheckedResult").ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
                    //执行更新
                    List<OL_RowDTO> updataData = _OL_ProRequirementsInterface.ConvertOL_Row(sourceDatas, _OL_ProRequirementsInterface.tableField);//需要更新的数据集合
                    await _OL_ProRequirementsInterface.ExecuteNoQuery<OL_RowDTO>(updataData, _OL_ProRequirementsInterface.upDateOption, _OL_ProRequirementsInterface.SqlDb);//执行更新


                }
            }
            else
            {
                throw new Exception("没有检验物料，请检查钉钉单号");

            }

        }


    }
}